package com.gitee.kamismile.stone.basic.data.domain.sysRole.service.impl;

import com.gitee.kamismile.stone.basic.data.domain.sysRole.repository.entity.SysRole;
import com.gitee.kamismile.stone.basic.data.domain.sysRole.repository.entity.SysRoleResources;
import com.gitee.kamismile.stone.basic.data.domain.sysRole.service.ISysRoleDomainService;
import com.gitee.kamismile.stone.basic.data.infrastructure.enums.RoleErrorEnum;
import com.gitee.kamismile.stone.basic.data.infrastructure.repository.r2Repository.sysRole.repository.SysRoleRepository;
import com.gitee.kamismile.stone.basic.data.infrastructure.repository.r2Repository.sysRole.repository.SysRoleResRepository;
import com.gitee.kamismile.stone.basic.data.infrastructure.utils.PageQueryBuilder;
import com.gitee.kamismile.stone.commmon.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

@Service
public class SysRoleDomainServiceImpl implements ISysRoleDomainService {

    @Autowired
    private R2dbcEntityTemplate template;

    @Autowired
    private SysRoleRepository sysRoleRepository;


    @Autowired
    private SysRoleResRepository sysRoleResRepository;


    @Override
    public Mono<SysRole> saveOrUpdate(SysRole sysRole) {
        return sysRoleRepository.save(sysRole);
    }

    @Override
    public Mono<Void> deleteSysRoleById(Integer nSysRoleId) {
        return sysRoleRepository.deleteById(nSysRoleId);
    }

    @Override
    public Mono<Page<SysRole>> findRolePage(int page, int size, Criteria criteria) {
       Pageable pageable = PageRequest.of(page, size);
        return new PageQueryBuilder<>(template, SysRole.class)
                .where(criteria)
                .pageable(pageable)
                .apply();
    }

    @Override
    public Mono<SysRole> findAndCheckRoleById(Integer nSysRoleId, Integer nTenantId) {
        return findRoleById(nSysRoleId, nTenantId)
                .switchIfEmpty(Mono.defer(() -> Mono.error(
                        new BusinessException(
                                RoleErrorEnum.RoleNotFound.getCode(),
                                RoleErrorEnum.RoleNotFound.getMessage()
                        ))));
    }

    public Mono<SysRole> findRoleById(Integer nSysRoleId, Integer nTenantId) {
        Assert.notNull(nSysRoleId, RoleErrorEnum.RoleNotFound.getMessage());
        SysRole example = new SysRole();
        example.setnSysRoleId(nSysRoleId);
        example.setnTenantId(nTenantId);
        return sysRoleRepository.findOne(Example.of(example));
    }

    @Override
    public Mono<SysRoleResources> findAndCheckRoleResById(Integer nSysRoleResourcesId, Integer nTenantId) {
        return findRoleResById(nSysRoleResourcesId, nTenantId)
                .switchIfEmpty(Mono.defer(() -> Mono.error(
                        new BusinessException(
                                RoleErrorEnum.RoleResNotFound.getCode(),
                                RoleErrorEnum.RoleResNotFound.getMessage()
                        ))));
    }


    public Mono<SysRoleResources> findRoleResById(Integer nSysRoleResourcesId, Integer nTenantId) {
        Assert.notNull(nSysRoleResourcesId, RoleErrorEnum.RoleResNotFound.getMessage());
        SysRoleResources example =new  SysRoleResources ();
        example.setnSysRoleResourcesId(nSysRoleResourcesId);
        example.setnTenantId(nTenantId);
        return sysRoleResRepository.findOne(Example.of(example));
    }
    @Override
    public Mono<SysRoleResources> saveOrUpdate(SysRoleResources sysRoleResources) {
        return sysRoleResRepository.save(sysRoleResources);
    }

    @Override
    public Mono<Void> deleteSysRoleResById(Integer nSysRoleResourcesId, Integer nTenantId) {
        return sysRoleResRepository.deleteById(nSysRoleResourcesId);
    }

    @Override
    public Mono<Page<SysRoleResources>> findRoleResPage(int page, int size, Criteria criteria) {
        Pageable pageable = PageRequest.of(page, size);
        return new PageQueryBuilder<>(template, SysRoleResources.class)
                .where(criteria)
                .pageable(pageable)
                .apply();
    }
}
